import { hasToken } from "@/utils/token";
import { Redirect, Route } from "react-router-dom";


export default function PrivateRoute({ children, ...rest }) {
    return (
        <Route {...rest}
            render={({ location }) => {
                if (hasToken()) {
                    // 登陆了
                    return children
                } else {
                    return (
                        <Redirect to={{
                            // 拦截到登录页的时候，需要把当前的路径传递过去，保证登录成功能够跳转回来
                            pathname: '/login',
                            state: { from: location.pathname }
                        }}></ Redirect >
                    )
                }
            }}
        ></Route >
    )
}